package com.wephone.Student.ui;

import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;

import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.GroupDetailsResponse;
import com.wephone.Student.bean.GroupResponse;
import com.wephone.Student.bean.data.GroupInfo;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.Util;


public class GroupJoinActivity extends BaseActivity {
	private GroupInfo groupData;
	private EditText join_group_desc;
	private Button btn_join;
	private ProgressDialog progressDialog;
	
	//获取详情
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(GroupJoinActivity.this,GroupResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				if(getDataSuccess){
					finish();
					Toast.makeText(getApplicationContext(), "申请提交成功", Toast.LENGTH_SHORT).show();
				}
			}
			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0, result);
				
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				Toast.makeText(getApplicationContext(), getString(R.string.network_error_txt), Toast.LENGTH_SHORT).show();
			}
		};
		return handler;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_join_group);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			groupData = (GroupInfo)bundle.getSerializable("groupData");
			init();
		}else{
			finish();
			Toast.makeText(getApplicationContext(), "数据异常", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void init(){
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		join_group_desc = (EditText)findViewById(R.id.join_group_desc);
		btn_join = (Button)findViewById(R.id.btn_join);
		btn_join.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				RequestParams params = new RequestParams();
				params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				params.put("desc",join_group_desc.getText().toString());
				progressDialog = ProgressDialog.show(GroupJoinActivity.this, "提示", "正在提交申请...", true, false);
				//HttpClient.getInstance().post(Constants.API_ADD_CHART_URL, params, getHandler());
				/*
				new Thread(new Runnable() {
					public void run() {
					*/
						//try {
							//EMGroupManager.getInstance().applyJoinToGroup(groupData.getGroupid(), join_group_desc.getText().toString());
						RequestParams params2 = new RequestParams();
						params2.put("group_id", groupData.getGroupid());
						params2.put("uId",BaseActivity.getMyInfo(GroupJoinActivity.this).getuId());
						params2.put("content", join_group_desc.getText().toString());
						
						HttpClient.getInstance().post(Constants.API_ADD_CHART_USER_URL, params2, new MyResponseHandler(GroupJoinActivity.this, BaseResponse.class){
							@Override
							public void onSuccess(String result) {
								super.onSuccess(result);
								if(progressDialog!=null){
									progressDialog.dismiss();
								}
								
								finish();
								Toast.makeText(getApplicationContext(), "申请提交成功", Toast.LENGTH_SHORT).show();
									
							}
						});
							
							/*
						} catch (EaseMobException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							runOnUiThread(new Runnable() {
								public void run() {
							if(progressDialog!=null){
								progressDialog.dismiss();
							}
							Toast.makeText(getApplicationContext(), "申请提交失败", Toast.LENGTH_SHORT).show();
								}});
						}
						*/
					//}
					//}).start();
			}
		});
	}	
	
}
